//
//  Demo1.c
//  Math06
//
//  Created by Xavier on 2023/11/15.
//

#include <stdio.h>
#include <stdlib.h>
//1.编写高级语言程序模拟trim方法，去除字符串两端的空格。
//例如String str = " math "; 删除str前后空格变为"math"。
typedef struct {
    char* ch;
    int length;
}String;

void StringCreate(String* Str, char* chars){
    //求长度
    int count = 0;
    while (chars[count] != '\0') {
        ++count;
    }
    Str->length = count;
    Str->ch = malloc(sizeof(char)*Str->length);
    for (int i = 0; i < count; i++) {
        Str->ch[i] = chars[i];
    }
    //printf("count:%d\n", count);
}

void StringPrint(String* Str){
    for (int i = 0; i < Str->length; i++) {
        printf("%c", Str->ch[i]);
    }
    printf("\n");
}

void StringTrim(String* Str) {
    int count=0,acount=0,bcount=0;
    int left = 0;
    int right = Str->length;
    int x = Str->length;
    for (int i = right; i >= (right+left)/2; i--) {
        if ((int)Str->ch[i] == 32) {
            ++acount;
        }
    }
    //printf("a:%d",count);
    int whileSize = 0;
    for (int i = left; i < (right+left)/2; i++) {
        if ((int)Str->ch[i] == 32) {
            ++bcount;
        }
        whileSize = bcount;
    }
    while (whileSize--) {
        for (int i = 0; i < Str->length; i++) {
            char tmp = Str->ch[i];
            Str->ch[i] = Str->ch[i+1];
            Str->ch[i+1] = tmp;
        }
    }
    count = acount+bcount;
    Str->length = Str->length - count;
}

//int main(int argc, const char * argv[]) {
//    String str;
//    StringCreate(&str, "  math  ");
//    StringPrint(&str);
//    StringTrim(&str);
//    StringPrint(&str);    
//}
